package com.abou.mvc.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.abou.constant.product.EProductType;
import com.abou.constant.product.ESortOrder;
import com.abou.model.common.product.search.SearchProductsRequest;
import com.abou.model.common.product.search.SearchProductsResult;
import com.abou.mvc.controller.ACommonController;
import com.abou.mvc.controller.ControllerUtils;

/**
 * The controller for the home page
 * 
 * @author khalid
 */

@Controller
@RequestMapping("/admin/home.do")
@SessionAttributes(ControllerUtils.SEARCH_REQUEST_ATT)
public class HomeController extends ACommonController {
  
  /**
   * The request mapping for home view
   */
  public static final String HOME_VIEW_REQUEST_MAPPING = "/admin/home.do";
  
  /**
   * The parameter value used to confirm a product creation
   */
  public static final String PRODUCT_CREATION_CONFIRMATION = "productCreation";
  
  /**
   * The parameter value used to confirm a product modification
   */
  public static final String PRODUCT_MODIFICATION_CONFIRMATION =
      "productModification";
  
  /**
   * The home view
   */
  public static final String VIEW_NAME = "admin/home";
  
  /**
   * The empty constructor
   */
  public HomeController() {

    super(VIEW_NAME, null);
  }
  
  /**
   * Shows the home page.
   * 
   * @param model The model
   * @return
   */
  @RequestMapping(method = RequestMethod.GET)
  @ModelAttribute(ControllerUtils.SEARCH_REQUEST_ATT)
  public SearchProductsRequest show(ModelMap model) {

    SearchProductsRequest searchRequest = new SearchProductsRequest();
    
    model.put(ControllerUtils.SEARCH_REQUEST_ATT, searchRequest);
    model.put(ControllerUtils.SEARCH_RESULT_ATT,
        getProductManager().listAllProducts(ESortOrder.ASC,
            searchRequest.getPageIndex(), searchRequest.getRowsNbByPage()));
    
    return searchRequest;
  }
  
  /**
   * Receives the form data and handles it.
   * 
   * @param searchProductsRequest The search request data
   * @param result The binding result
   * @param request The HTTP request
   * @param rowsNbModif If not empty, the request modifies the rows number per
   *          page in the table
   * @return The search result
   */
  @RequestMapping(method = RequestMethod.POST)
  @ModelAttribute(ControllerUtils.SEARCH_RESULT_ATT)
  public SearchProductsResult submitForm(
      @ModelAttribute(ControllerUtils.SEARCH_REQUEST_ATT) SearchProductsRequest searchProductsRequest) {

    SearchProductsResult searchResult = null;
    
    EProductType productType = searchProductsRequest.getProductType();
    String name = searchProductsRequest.getName();
    
    if (searchProductsRequest.getProductType() == null) {
      
      searchResult =
          getProductManager().findProductByName(name, ESortOrder.ASC,
              searchProductsRequest.getPageIndex(),
              searchProductsRequest.getRowsNbByPage());
      
    } else {
      
      searchResult =
          getProductManager().listProducts(name, "", productType,
              ESortOrder.ASC, searchProductsRequest.getPageIndex(),
              searchProductsRequest.getRowsNbByPage());
    }
    
    return searchResult;
  }
}
